package com.onec.service.auth.dto;

import com.onec.service.api.enums.StatusEnum;
import com.onec.service.auth.entity.CustomerInfo;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;

/**
 * @author ONEC
 */
@Data
public class CustomerDto implements UserDetails, Serializable {
    /**
     * id
     */
    private Long id;


    private String username;

    private String clientId;


    private String customerNo;

    private String phone;

    private Long merchantId;

    private String email;

    private String password;

    private String remark;

    private boolean enabled;

    private Collection<SimpleGrantedAuthority> authorities;

    public CustomerDto(CustomerInfo user, Collection<SimpleGrantedAuthority> auth) {
        this.setId(user.getId());
        this.setUsername(user.getUsername());
        //this.setPassword(AuthConstants.BCRYPT + user.getPassword());
        this.setPassword(user.getPassword());
        this.setEnabled(StatusEnum.isSuccess(user.getStatus()));
        this.setClientId(user.getClientId());
        this.authorities = auth;
//        if (!auth.isEmpty()) {
//            this.authorities = new ArrayList<>(auth.size());
//            auth.forEach(roleId -> authorities.add(new SimpleGrantedAuthority(String.valueOf(roleId))));
//        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override


    public boolean isEnabled() {
        return this.enabled;
    }


}
